package com.base.pig.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.base.pig.service.PskInfoService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;


import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 乐橙云开放接口工具
 */
public class LechangeHttpSend {

    public static String HOST = "https://openapi.lechange.cn:443/openapi/";

    public static String QYY_HOST = "imou://open.lechange.com/";

    public static String APPID = "lc886365b9c4194e40";

    public static String SECRET = "3bc447e8dfb5442e92454954884d5e";

    public static final String LECHANGE_ACCESS_TOKEN = "LECHANGE_ACCESS_TOKEN";

    //获取accessToken
    public static String getAccessToken() {
        String method = "accessToken";
        Map<String, Object> paramMap = new HashMap<>();
        JSONObject json = LechangeHttpSend.execute(paramMap, method);
        JSONObject jsonResult = json.getJSONObject("result");
        JSONObject jsonData = jsonResult.getJSONObject("data");
        return jsonData.getString("accessToken");
    }

    //查询设备本地录像片段
    public static JSONObject queryLocalRecords(Map<String, Object> paramMap) {
        String method = "queryLocalRecords";
        paramMap.put("type","All");
        paramMap.put("count",100);
        JSONObject json = LechangeHttpSend.execute(paramMap, method);
        JSONObject jsonResult = json.getJSONObject("result");
        return jsonResult.getJSONObject("data");
    }

    //云台移动控制
    public static JSONObject controlMovePTZ(Map<String, Object> paramMap) {
        String method = "controlMovePTZ";
        JSONObject json = LechangeHttpSend.execute(paramMap, method);
        JSONObject jsonResult = json.getJSONObject("result");
        return jsonResult.getJSONObject("data");
    }

    //获取KitToken
    public static JSONObject getKitToken(Map<String, Object> paramMap) {
        String method = "getKitToken";
        paramMap.put("type","0");
        JSONObject json = LechangeHttpSend.execute(paramMap, method);
        JSONObject jsonResult = json.getJSONObject("result");
        return jsonResult.getJSONObject("data");
    }

    public static JSONObject execute(Map<String, Object> paramsMap, String method) {
        JSONObject map = paramsInit(paramsMap);
        // 返回json
        return JSONObject.parseObject(HttpUtils.HttpUtils_RequestPost(HOST + method, map));
    }



    protected static JSONObject paramsInit(Map<String, Object> paramsMap) {
        long time = System.currentTimeMillis() / 1000;
        String nonce = UUID.randomUUID().toString();
        String id = UUID.randomUUID().toString();

        StringBuilder paramString = new StringBuilder();
        paramString.append("time:").append(time).append(",");
        paramString.append("nonce:").append(nonce).append(",");
        paramString.append("appSecret:").append(SECRET);

        String sign = "";
        // 计算MD5得值
        try {
            sign = DigestUtils.md5Hex(paramString.toString().trim().getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        JSONObject systemMap = new JSONObject();
        systemMap.put("ver", "1.0");
        systemMap.put("sign", sign);
        systemMap.put("appId", APPID);
        systemMap.put("nonce", nonce);
        systemMap.put("time", time);

        JSONObject map = new JSONObject();
        map.put("system", systemMap);
        map.put("params", paramsMap);
        map.put("id", id);
        return map;
    }

}
